package com.ccl.iot.huiju.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ccl.iot.R;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.dvr.utils.GlideUtil;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.huiju.EditItemFragment;
import com.ccl.iot.huiju.IOTHuiJu;
import com.ccl.iot.views.MarqueeLabel;

/**
 * @description：
 * @author： HD
 * @time：2017//22
 */

public class PositionItemAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private SparseArray<IOTHuiJu> mDatas;
    public Context mContext;
    private int mDatasize;

    @Override
    public int getCount() {
        return 6;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_position, null);

            viewHolder.mSelectLayout = (LinearLayout) convertView.findViewById(R.id.layout_main_item);
            viewHolder.mItemImg = (ImageView) convertView.findViewById(R.id.item_iamge);
            viewHolder.mItemNameTv = (MarqueeLabel) convertView.findViewById(R.id.item_name);

            viewHolder.mSelectAddImg = (ImageView) convertView.findViewById(R.id.iv_select);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        IOTHuiJu iotHuiJu = mDatas.get(position);

        if (iotHuiJu != null) {
            viewHolder.mItemNameTv.setTextColor(Color.WHITE);
            viewHolder.mSelectLayout.setVisibility(View.VISIBLE);
            GlideUtil.loadImageView(mContext, R.drawable.icon_select_change, viewHolder.mSelectAddImg);

            if (iotHuiJu instanceof IOTDevice) {
                final IOTDevice device = (IOTDevice) iotHuiJu;
                int imageId = device.getImageId();
                if (imageId != 0) {
                    GlideUtil.loadImageView(mContext, imageId, viewHolder.mItemImg);
                }
                viewHolder.mItemNameTv.setText(device.getName());

            }
            if (iotHuiJu instanceof IOTBatch) {
                final IOTBatch iotBatch = (IOTBatch) iotHuiJu;
                int imgIndex = iotBatch.getImgIndex();
                if (imgIndex < 0 || imgIndex >= Consts.BATCH_IMG_IDS.size()) {
                    imgIndex = 0;
                }
                GlideUtil.loadImageView(mContext, Consts.BATCH_IMG_IDS.get(imgIndex), viewHolder.mItemImg);
                viewHolder.mItemNameTv.setText(iotBatch.getName());

            }
        } else {
            viewHolder.mSelectLayout.setVisibility(View.GONE);
            GlideUtil.loadImageView(mContext, R.drawable.icon_select_add, viewHolder.mSelectAddImg);
        }


        viewHolder.mSelectAddImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.i(EditItemFragment.TAG, "onClick-----------");
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });



        return convertView;
    }

    public static class ViewHolder {

        public LinearLayout mSelectLayout;
        public ImageView mItemImg;
        public MarqueeLabel mItemNameTv;
        public ImageView mSelectAddImg;

    }

    public PositionItemAdapter(Context context, SparseArray<IOTHuiJu> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
        mDatasize = datas.size();
    }

    public void notifyDataSetChanged(SparseArray<IOTHuiJu> datas) {
        mDatas = datas;
        mDatasize = datas.size();
        LogUtil.i(EditItemFragment.TAG, "size:" + mDatasize);
        notifyDataSetChanged();
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    protected OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }


}